<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;

/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class ReportController extends HomeController {



    public function login()
    {
        //获取登录信息
        if (IS_POST) {
            $username=I('post.username');
            $password=I('post.password');
            $uid = D('ReportMember')->login($username, $password);
            if(0 < $uid){ //UC登录成功
                $this->success('登录成功！', U('studentReport'));
            } else { //登录失败
                if($uid==-1){
                    $this->error('用户不存在！', '', true);
                }else{
                    $this->error('密码不正确！', '', true);
                }
            }

        }else{
            if(is_Reportlogin()){
                $this->redirect('studentReport');
            }else{
                $this->display();
            }
        }

    }


    //学生入学报道
    public  function studentReport(){
        if(IS_POST){
            $data=I('post.');
            $data['create_time']=time();
            $data['is_report']=1;
            $student=M('student_info')
                    ->where(array('ID_number'=>$data['ID_number']))
                    ->field('id,name,ID_number')
                    ->find();   
            if(empty($student)){ //新增数据
                $data['create_time']=time();
                $data['update_time']=time();
                $data['type']=4;
                $res = M('student_info')->add($data);
                if(!$res){
                    $this->error ("提交出错");
                }else{
                    $this->success ('提交成功！');
                }
            } else { //更新数据
                $data['update_time']=time();
                $res = M('student_info')->save($data);
                if(!$res){
                    $this->error ("提交出错");
                }else{
                    $this->success ('提交成功！');
                }
            }

        }else{
            if(!is_Reportlogin()){
                $this->redirect('login');
            }else{
                $major=M('major')->field('id,title')->select();
                $this->assign('major',$major);
                $class=M('class')->field('id,title')->select();
                $this->assign('class',$class);
                $this->display();
            }
        }
    }

    //获取学生信息
    public function getStudentInfo(){
        if(IS_POST){
            I('post.name')?$where['name']=I('post.name'):'';
            I('post.ID_number')?$where['ID_number']=I('post.ID_number'):'';
            if(isset($where)){
                $student=M('student_info')
                    ->where($where)
                    ->field('id,name,ID_number,mobile,home_address,class_id,school,major')
                    ->find();
                $this->ajaxReturn(['status'=>1,'info'=>$student]);
            }else{
                $this->error ("数据错误");
            }
        }
    }

    /* 退出登录 */
    public function logout(){
        if(is_login()){
            session('user_auth', null);
            session('user_auth_sign', null);
            $this->redirect('Index/login');
        } else {
            $this->redirect('Index/login');
        }
    }



}